package br.com.Formula;

import br.com.ConexaoBanco.ConexaoMySQL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * CadastrarUsuario.java
 *
 * Created on 24/05/2011, 08:27:54
 */
/**
 *
 * @author Jayme Augusto Vara
 */
public class CadastrarEquipe extends javax.swing.JInternalFrame {

    /**
     * Creates new form CadastrarUsuario
     */
    public CadastrarEquipe() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblID = new javax.swing.JLabel();
        btnSalvar = new javax.swing.JButton();
        jtfID = new javax.swing.JTextField();
        lblNome = new javax.swing.JLabel();
        jtfNome = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Cadastro de Equipes");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/icone/16 x 16/add.png"))); // NOI18N

        lblID.setText("ID:");

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icone/filesave.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        btnSalvar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnSalvarKeyPressed(evt);
            }
        });

        lblNome.setText("Nome:");

        jtfNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfNomeKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSalvar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblID)
                            .addComponent(lblNome))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfID, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                            .addComponent(jtfNome))))
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblID)
                    .addComponent(jtfID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSalvar)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void SalvaDados() {
        if (jtfID.getText().equals("") || jtfNome.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Campos obrigatórios não preenchidos",
                    "Campos obrigatórios não preenchidos", 1);
            jtfID.requestFocus();
        } else {
            try {
                //Abre a conexao com o banco de dados
                Connection com = ConexaoMySQL.getConexaoMySQL();
                Statement stm = com.createStatement();
                //Insere no banco os dados
                stm.executeUpdate("INSERT INTO equipe VALUES ('"
                        + jtfID.getText() + "','" + jtfNome.getText() + "')");
                JOptionPane.showMessageDialog(null, "Dados salvos com sucesso!",
                        "Dados salvos com sucesso!", 1);
                //Fecha a conexao
                ConexaoMySQL.FecharConexao();
                //Deixa os campos em branco
                jtfID.setText("");
                jtfNome.setText("");
                jtfID.requestFocus();
            } catch (SQLException ex) {
                Logger.getLogger(CadastrarEquipe.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        SalvaDados();
}//GEN-LAST:event_btnSalvarActionPerformed

    private void btnSalvarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnSalvarKeyPressed
        //Pega o codigo do botão precionado
        int ln_ke_pressionado = evt.getKeyCode();
        //Testa o codigo pra ver se é 10 (Enter), se for, chama função salva dados
        if (ln_ke_pressionado == 10) {
            SalvaDados();
        }
    }//GEN-LAST:event_btnSalvarKeyPressed

        private void jtfNomeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfNomeKeyPressed
            //Pega o codigo do botão precionado
            int ln_ke_pressionado = evt.getKeyCode();
            //Testa o codigo pra ver se é 10 (Enter), se for, chama função salva dados
            if (ln_ke_pressionado == 10) {
                SalvaDados();
            }
    }//GEN-LAST:event_jtfNomeKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JTextField jtfID;
    private javax.swing.JTextField jtfNome;
    private javax.swing.JLabel lblID;
    private javax.swing.JLabel lblNome;
    // End of variables declaration//GEN-END:variables
}
